<template>
    <div class="detail_l">
        <div class="title">
            <h2>
                <i class="mv"></i>
                {{ name }}
                <span class="name" @click="$router.push(`/artlist/${artistId}`)" href="#view">{{ artistName }}</span>
            </h2>
        </div>
        <!-- :videoCover="videoCover" -->
        <VideoPlay
          v-show="true"
          :videoUrl="videoUrl"
          :width="640"
          :height="360"
          :autoplay="true"
          :controls="true"
          :loop="false"
          :muted="false"
          preload="auto"
          :showPlay="true"
          :playWidth="96"
          zoom="cotain"
        />
        <div class="comment_btns">
            <a class="comment" title="评论" @click="viewClick()">
              <span>({{ commentCount || '评论' }})</span>
            </a>
            <a class="dis" title="收藏">
                <span>({{ subCount || '收藏' }})</span>
            </a>
            <a class="share" title="分享">
                <span>({{ shareCount || '分享' }})</span>
            </a>
        </div>
        <div class="comment_title" id="view" >
            <h2>评论</h2>
            <span>共{{ commentCount }}条评论</span>
        </div>
        <div class="comment">
            <div class="com_l">
                <img src="http://p1.music.126.net/B8zVoyFfiBy6hnrLy3KTWg==/109951167913480051.jpg?param=200y200" alt="" v-if="$store.state.isHeader">
                <img src="http://s4.music.126.net/style/web2/img/default/default_avatar.jpg?param=50y50" alt="" v-else>
            </div>
            <div class="com_r">
                <textarea name="" id="" placeholder="评论" v-model="content"></textarea>
                <div class="btns">
                    <div><i class="smile"></i><i class="alt"></i></div>
                    <div><span class="count">{{contentMaxLength - content.length}}</span><a @click="viewBtn">评论</a></div>
                </div>
            </div>
        </div>
        <div class="comment_list">
            <h3 v-show="hotComments.length==0?false:true">精彩评论</h3>
            <ul>
                <li v-for="(item, index) in hotComments" :key="index">
                    <img :src="item.user.avatarUrl" alt="">
                    <div class="cnt">
                        <div class="cnt_t">
                            <p class="text">
                                <span class="name" @click="$router.push(`/user/${item.user.userId}`)">
                                    {{ item.user.nickname }}:
                                </span>
                                {{ item.content }}
                            </p>
                        </div>
                        <div class="cnt_c" v-for="(item1, index) in item.beReplied" :key="index">
                            <p class="text" >
                                <span class="name" @click="$router.push(`/user/${item1.user.userId}`)">
                                    {{ item1.user.nickname }}:
                                </span>
                                {{ item1.content }}
                            </p>
                        </div>
                        <div class="cnt_c" v-for="(item1, index) in item.beReplied" :key="index">
                            <p class="text" >
                                <span class="name">
                                    {{ item1.user.nickname }}:
                                </span>
                                {{ item1.content }}
                            </p>
                        </div>
                        <div class="cnt_b">
                            <span class="time">{{ item.time | showDate }}</span>
                            <span class="zan">
                                <i class="good" v-show="!item.liked" @click="likeFn(item)"></i>
                                <i class="like" v-show="item.liked" @click="unlikeFn(item)"></i>
                                <i class="count">({{ item.likedCount }})</i>
                                <i class="sep">|</i>
                                <i class="view" @click="replyFn(item)">回复</i>
                            </span>
                        </div>
                        <!-- 回复区域 -->
                        <div class="reply" v-show="item.owner">
                            <textarea v-model="recontent" ></textarea>
                            <div class="btns">
                                <div><i class="smile"></i><i class="alt"></i></div>
                                <div><span class="count">{{contentMaxLength - recontent.length}}</span><a @click="replyBtn(item)">回复</a></div>
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
            <h3 v-show="comments.length==0?false:true">最新评论</h3>
            <ul>
                <li v-for="(item, index) in comments" :key="index">
                    <img :src="item.user.avatarUrl" alt="">
                    <div class="cnt">
                        <div class="cnt_t">
                          <p class="text" >
                                <span class="name" @click="$router.push(`/user/${item.user.userId}`)">
                                    {{ item.user.nickname }}:
                                </span>
                                {{ item.content }}
                            </p>
                        </div>
                        <div class="cnt_c" v-for="(item1, index) in item.beReplied" :key="index">
                            <p class="text" >
                                <span class="name" @click="$router.push(`/user/${item1.user.userId}`)">
                                    {{ item1.user.nickname }}:
                                </span>
                                {{ item1.content }}
                            </p>
                        </div>
                        <div class="cnt_b">
                            <span class="time">{{ item.timeStr }}</span>
                            <span class="zan">
                                <i class="good" v-show="!item.liked" @click="likeFn(item)"></i>
                                <i class="like" v-show="item.liked" @click="unlikeFn(item)"></i>
                                <i class="count">({{ item.likedCount }})</i>
                                <i class="sep">|</i>
                                <i class="view" @click="replyFn(item)">回复</i>
                            </span>
                        </div>
                        <!-- 回复区域 -->
                        <div class="reply" v-show="item.owner">
                            <textarea placeholder="回复" v-model="recontent"></textarea>
                            <div class="btns">
                                <div><i class="smile"></i><i class="alt"></i></div>
                                <div><span class="count">{{contentMaxLength - recontent.length}}</span><a @click="replyBtn(item)">回复</a></div>
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import VideoPlay from '@/components/VideoPlay.vue';
import { formatDate } from '@/assets/utils/date';
import { MvBfApi, MvDtailApi, MvCommentApi, DetailViewApi, DetailLikeApi } from '@/request/api';
export default {
    data () {
        return {
            ids: 0,
            videoUrl: "",
            // videoCover: "",
            name: "",
            artistId: "",
            artistName: "",
            subCount: 0,
            shareCount: 0,
            commentCount: 0,
            hotComments: [],
            comments: [],
            content: "",
            recontent: "",
            contentMaxLength: 140,
        }
    },
    components: {
        VideoPlay,
    },
    filters: {
      showDate: function(value) {
        let date = new Date(value);
        // return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
        return formatDate(date, 'yyyy年MM月dd日');
      }
    },
    computed: {
        id() {
          return this.$route.params.id;
        },
    },
    created() {
        MvBfApi(this.id).then((res)=> {
            // console.log(res);
            this.videoUrl = res.data.data.url;
        });
        MvDtailApi(this.id).then((res) => {
            // console.log(res);
            this.name = res.data.data.name;
            this.artistId = res.data.data.artistId;
            this.artistName = res.data.data.artistName;
            this.subCount = res.data.data.subCount;
            this.shareCount = res.data.data.shareCount;
            this.commentCount = res.data.data.commentCount;
        });
        // mv评价
        MvCommentApi(this.id).then((res) => {
            // console.log(res);
            this.commentCount = res.data.total;
            this.hotComments = res.data.hotComments;
            this.comments = res.data.comments;
        });
    },
    methods: {
        viewClick() {
          document.querySelector('#view').scrollIntoView({
            behavior: "auto",
            block: "center",
            inline: "nearest"
          })
        },
        // 评论
        viewBtn() {
          if(this.content.length === 0){
            let obj = { content: "请输入点内容再提交!", icon: "danger" };
            this.$store.dispatch("AsyncToast", obj);
          }else if(this.content.length <= this.contentMaxLength) {
            DetailViewApi({
              t: 1,
              type: 1,
              id: this.id,
              content: this.content,
            }).then((res) => {
              // console.log(res);
              if(res.data.code === 302){
                // 检验评论成功
                let obj = { content: "评论成功!", icon: "success" };
                this.$store.dispatch("AsyncToast", obj);
              }else{
                // 检验发送失败
                let obj = { content: "评论失败!", icon: "danger" };
                this.$store.dispatch("AsyncToast", obj);
              }
            })
            this.content = "";
            this.commentCount = this.commentCount + 1;
          }else{
            let obj = { content: "输入不能超过140字符!", icon: "danger" };
            this.$store.dispatch("AsyncToast", obj);
          }
        },
        // 点赞
        likeFn(item) {
          if(item.liked === false){
            DetailLikeApi({
              t: 1,
              type: 1,
              id: this.id,
              cid: item.commentId,
            }).then((res) => {
              // console.log(res);
              if(res.data.code === 302){
                let obj = { content: "点赞成功!", icon: "success" };
                this.$store.dispatch("AsyncToast", obj);
              }else{
                let obj = { content: "点赞失败！", icon: "danger" };
                this.$store.dispatch("AsyncToast", obj);
              }
            })
            item.liked = true;
            item.likedCount = item.likedCount + 1;
          }else{
            let obj = { content: "已点赞！", icon: "danger" };
            this.$store.dispatch("AsyncToast", obj);
          }
        },
        // 取消点赞
        unlikeFn(item) {
          if(item.liked === true){
            DetailLikeApi({
              t: 0,
              type: 1,
              id: this.id,
              cid: item.commentId,
            }).then((res) => {
              // console.log(res);
              if(res.data.code === 302){
                let obj = { content: "取消点赞成功!", icon: "success" };
                this.$store.dispatch("AsyncToast", obj);
              }else{
                let obj = { content: "取消点赞失败！", icon: "danger" };
                this.$store.dispatch("AsyncToast", obj);
              }
            })
            item.liked = false;
            item.likedCount = item.likedCount - 1;
          }else{
            let obj = { content: "已取消点赞！", icon: "danger" };
            this.$store.dispatch("AsyncToast", obj);
          }
        },
        // 回复
        replyFn(item) {
          if(item.owner === false){
            this.hotComments.forEach(item=>item.owner=false);
            this.comments.forEach(item=>item.owner=false);
            item.owner = true;
            this.recontent = '回复'+item.user.nickname+':';
          }else{
            item.owner = false;
          }
        },
        // 回复评论
        replyBtn(item) {
          // console.log(item.user.nickname.length+3);
          if(this.recontent.length === item.user.nickname.length+3){
            let obj = { content: "请输入点内容再提交!", icon: "danger" };
            this.$store.dispatch("AsyncToast", obj);
          }else if(this.recontent.length <= this.contentMaxLength){
            DetailViewApi({
              t: 2,
              type: 1,
              id: this.id,
              content: this.recontent,
              commentId: item.commentId,
            }).then((res) => {
              // console.log(res);
              if(res.data.code === 302){
                // 检验回复成功
                let obj = { content: "回复成功!", icon: "success" };
                this.$store.dispatch("AsyncToast", obj);
              }else{
                // 检验回复失败
                let obj = { content: "回复失败!", icon: "danger" };
                this.$store.dispatch("AsyncToast", obj);
              }
            })
            item.owner = false;
          }else{
            let obj = { content: "输入不能超过140字符!", icon: "danger" };
            this.$store.dispatch("AsyncToast", obj);
          }
        },
    }
}
</script>
 
<style lang = "less" scoped>
@import "../../assets/base";
.detail_l {
    width: 640px;
    background-color: #fff;
    border-left: 1px solid #D7D7D7;
    padding: 40px;
    .title {
        display: flex;
        align-items: center;
        // 超出换行
        white-space: pre-wrap;
        .mv {
            width: 29px;
            height: 18px;
            display: inline-block;
            background: url("@/assets/images/public/icon2.png");
            background-position: -230px -480px;
        }
        h2 {
            font-size: 24px;
            color: #333;
            margin-right: 10px;
            padding-bottom: 10px;
        }
        .name {
            font-size: 12px;
            color: #0C73C2;
            &:hover {
                cursor: pointer;
                text-decoration: underline;
            }
        }
    }
    .comment_btns {
        height: 31px;
        display: flex;
        margin: 10px 0 45px;
        .dis {
            margin-right: 6px;
            height: 100%;
            background: url("../../assets/images/public/button2.png") no-repeat;
            background-position: 0px -1063px;
            span {
              display: inline-block;
              height: 100%;
              background: url("../../assets/images/public/button2.png") no-repeat right -1106px;
              line-height: 31px;
              padding-left: 30px;
              padding-right: 4px;
              font-size: 12px;
            }
            &:hover {
              color: #666;
              cursor: pointer;
            }
        }
        .share {
            height: 31px;
            margin-right: 6px;
            float: left;
            min-width: 23px;
            cursor: pointer;
            background: url("../../assets/images/public/button2.png") no-repeat;
            background-position: 0 -1225px;
            span {
              height: 100%;
              display: inline-block;
              line-height: 31px;
              padding-left: 28px;
              padding-right: 4px;
              background: url("../../assets/images/public/button2.png") no-repeat;
              background-position: right -1020px;
              font-size: 12px;
            }
            &:hover {
              color: #666;
              cursor: pointer;
            }
        }
        .comment {
            margin-right: 5px;
            height: 31px;
            min-width: 23px;
            cursor: pointer;
            background: url("../../assets/images/public/button2.png") no-repeat;
            background-position: 0 -1465px;
            span {
                height: 100%;
                display: inline-block;
                line-height: 31px;
                padding-left: 28px;
                padding-right: 8px;
                background: url("../../assets/images/public/button2.png") no-repeat;
                background-position: right -1020px;
                font-size: 12px;
                color: #333;
            }
            &:hover {
                color: #666;
                cursor: pointer;
            }
        }
    }
    .comment_title {
        height: 40px;
        width: 640px;
        border-bottom: 2px solid @red;
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        h2 {
            font-size: 20px;
            color: #333;
            cursor: pointer;
            &:hover {
                text-decoration: underline;
            }
        }
        span {
            font-size: 12px;
            color: #666;
            margin: 9px 0 0 20px;
        }
    }
    .comment {
        display: flex;
        font-size: 12px;
        .com_l {
            height: 100px;
            margin-right: 10px;
            img {
                width: 50px;
                height: 50px;
            }
        }
        .com_r {
            height: 100px;
            textarea {
                resize:none;
                width: 565px;
                height: 50px;
                padding: 5px 6px 6px;
                border: 1px solid #cdcdcd;
                border-radius: 2px;
                line-height: 19px;
                box-sizing: content-box;
            }
            .btns {
                padding: 10px 0 0;
                display: flex;
                justify-content: space-between;
                .smile {
                    cursor: pointer;
                    width: 18px;
                    height: 18px;
                    display: inline-block;
                    background: url("@/assets/images/public/icon.png") no-repeat;
                    background-position: -40px -490px;
                    margin-right: 10px;
                }
                .alt {
                    cursor: pointer;
                    width: 18px;
                    height: 18px;
                    display: inline-block;
                    background: url("@/assets/images/public/icon.png") no-repeat;
                    background-position: -60px -490px;
                }
                .count {
                    font-size: 12px;
                    color: #999;
                    margin-right: 10px;
                }
                a {
                    width: 46px;
                    height: 25px;
                    display: inline-block;
                    background: url("@/assets/images/public/button.png") no-repeat;
                    background-position: -84px -64px;
                    color: #fff;
                    text-align: center;
                    line-height: 25px;
                    &:hover {
                        cursor: pointer;
                        background-position: -84px -94px;
                    }
                }
            }
        }
    }
    .comment_list {
        h3 {
            height: 20px;
            width: 640px;
            font-weight: bolder;
            font-size: 12px;
            color: #333;
            border-bottom: 1px solid #ccc;
            display: flex;
            align-items: center;
            margin-top: 20px;
        }
        ul {
            li {
                width: 640px;
                padding: 15px 0;
                display: flex;
                border-bottom: 1px dotted #999;
                &:last-child {
                    border: 0;
                }
                img {
                    cursor: pointer;
                    width: 50px;
                    height: 50px;
                }
                .cnt {
                    font-size: 12px;
                    margin-left: 10px;
                }
                .cnt_t {
                    width: 580px;
                    display: flex;
                    align-items: center;
                    .name {
                        line-height: 20px;
                        color: #0C73C2;
                        &:hover {
                            cursor: pointer;
                            text-decoration: underline;
                            color: #0C73C2;
                        }
                    }
                    .text {
                        line-height: 20px;
                        color: #333;
                    }
                }
                .cnt_c {
                    width: 540px;
                    padding: 8px 20px;
                    margin-top: 10px;
                    line-height: 20px;
                    background: #f4f4f4;
                    border: 1px solid #dedede;
                    display: flex;
                    align-items: center;
                    .name {
                        line-height: 20px;
                        color: #0C73C2;
                        &:hover {
                            cursor: pointer;
                            text-decoration: underline;
                            color: #0C73C2;
                        }
                    }
                    .text {
                        line-height: 20px;
                        color: #333;
                    }
                }
                .cnt_b {
                    width: 580px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    margin: 15px 0;
                    .time {
                        color: #999;
                    }
                    .zan {
                        .good {
                            width: 15px;
                            height: 14px;
                            display: inline-block;
                            background: url("@/assets/images/public/icon2.png") no-repeat;
                            background-position: -150px 0;
                            &:hover {
                                cursor: pointer;
                                background-position: -150px -20px;
                            }
                        }
                        .like {
                            width: 15px;
                            height: 14px;
                            display: inline-block;
                            background: url("@/assets/images/public/icon2.png") no-repeat;
                            background-position: -170px 0;
                        }
                        .count {
                            margin-left: 8px;
                            color: #333;
                            &:hover {
                                cursor: pointer;
                                text-decoration: underline;
                                color: #333;
                            }
                        }
                        .sep {
                            margin: 0 8px;
                            color: #ccc;
                        }
                        .view {
                            color: #666;
                            &:hover {
                                cursor: pointer;
                                text-decoration: underline;
                                color: #666;
                            }
                        }
                    }
                }
                .reply {
                  width: 550px;
                  padding: 15px;
                  background: #f8f8f8;
                  border: 1px solid #dedede;
                  border-radius: 2px;
                  box-sizing: content-box;
                  textarea {
                    resize:none;
                    width: 540px;
                    height: 20px;
                    padding: 5px 6px 6px;
                    border: 1px solid #cdcdcd;
                    border-radius: 2px;
                    line-height: 20px;
                    text-align: start;
                  }
                  .btns {
                      padding: 10px 0 0;
                      display: flex;
                      justify-content: space-between;
                      .smile {
                          cursor: pointer;
                          width: 18px;
                          height: 18px;
                          display: inline-block;
                          background: url("@/assets/images/public/icon.png") no-repeat;
                          background-position: -40px -490px;
                          margin-right: 10px;
                      }
                      .alt {
                          cursor: pointer;
                          width: 18px;
                          height: 18px;
                          display: inline-block;
                          background: url("@/assets/images/public/icon.png") no-repeat;
                          background-position: -60px -490px;
                      }
                      .count {
                          font-size: 12px;
                          color: #999;
                          margin-right: 10px;
                      }
                      a {
                          width: 46px;
                          height: 25px;
                          display: inline-block;
                          background: url("@/assets/images/public/button.png") no-repeat;
                          background-position: -84px -64px;
                          color: #fff;
                          text-align: center;
                          line-height: 25px;
                          &:hover {
                              cursor: pointer;
                              background-position: -84px -94px;
                          }
                      }
                  }
                }
            }
        }
    }
}
</style>